<template>
  <view>
    <view class="goods-list">
      <view v-for="(goods, i) in goodsList" :key="i" @click="gotoDetail(goods)">
        <my-goods :goods="goods"></my-goods></view>
    </view>
  </view>
</template>

<script>
export default {
  name: '',
  components: {},
  data() {
    return {
      queryObj: {
        // 查询关键词
        query: '',
        // 商品分类Id
        cid: '',
        // 页码值
        pagenum: 1,
        // 每页显示多少条数据
        pagesize: 10
      },
      goodsList: [],
      total: 0,
      isLoading: false
    };
  },
  created() {},
  mounted() {},
  onLoad(options) {
    this.queryObj.query = options.query || '';
    this.queryObj.cid = options.cid || '';
    this.getGoodsList();
  },
  methods: {
    async getGoodsList() {
      try {
        this.isLoading = true;
        let { data: res } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj);
        if (res.meta.status !== 200) {
          return uni.$showMsg();
        }
        this.goodsList = [...this.goodsList, ...res.message.goods];
        this.total = res.message.total;
      } catch (e) {
        console.log(e);
      }
      this.isLoading = false;
    },
    gotoDetail(item) {
      uni.navigateTo({
        url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
      });
    }
  },
  onReachBottom() {
    if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕！');
    if (this.isLoading) return;
    // 让页码值自增 +1
    // 重新获取列表数据
    this.queryObj.pagenum += 1
    this.getGoodsList();
  },
  async onPullDownRefresh() {
    // 1. 重置关键数据
    this.queryObj.pagenum = 1;
    this.total = 0;
    this.isloading = false;
    this.goodsList = [];

    // 2. 重新发起请求
    await this.getGoodsList();
    uni.stopPullDownRefresh();
  }
};
</script>

<style lang="scss"></style>
